<template>
  <div class="userList back-white">
    <el-container>
      <el-main>
        <el-form>
          <!--<el-form-item label="管理员账号"><el-input v-model="account"></el-input></el-form-item>-->
          <!--<el-form-item label="管理员姓名"><el-input v-model="account"></el-input></el-form-item>-->
          <el-form-item><el-button type="primary" @click="$router.push('/adminEdit')">添加账号</el-button></el-form-item>
        </el-form>
        <div class="x-item x-item-none back-white">
            <el-table border :data="list">
              <el-table-column label="id" prop="id" width="80px"></el-table-column>
              <el-table-column label="账号" prop="admin_account"></el-table-column>
              <el-table-column label="姓名" prop="admin_name"></el-table-column>
              <el-table-column label="邮箱" prop="email"></el-table-column>
              <el-table-column label="添加时间" prop="create_time"></el-table-column>
              <el-table-column label="最后登录" prop="login_time"></el-table-column>
              <el-table-column label="账号启用状态" prop="is_delete">
                <template slot-scope="scope">
                  <el-tag v-if="scope.row.status == 0">启用</el-tag>
                  <el-tag type="danger"  v-if="scope.row.status == 1">禁用</el-tag>
                </template>
              </el-table-column>
              <el-table-column label="操作" prop="status">
                <template slot-scope="scope">
                  <el-button type="text" size="small" v-if="scope.row.status == 1" @click="toggleAccount(0,scope.row.id)">启用</el-button>
                  <el-button type="text" size="small" v-if="scope.row.status == 0" @click="toggleAccount(1,scope.row.id)">禁用</el-button>
                  <el-button type="text" size="small" @click="adminDelete(scope.row.id)">删除</el-button>
                  <el-button type="text" size="small" @click="toEdit(scope.row.id)">编辑</el-button>
                </template>
              </el-table-column>
            </el-table>
          <div class="pdtb075 tc">
            <el-pagination
              background
              layout="prev, pager, next"
              @current-change="pageChange"
              :total="pageCount">
            </el-pagination>
          </div>
          </div>
      </el-main>
    </el-container>
  </div>
</template>
<script type="text/ecmascript-6">
  import {getDateList,createData} from '../../api/api';
  export default {
    data(){
      return {
        account: '', //管理员账号
        name: '', //管理员姓名
        list: [], //用户列表
        pageCount: 0, //总数
        curPage:1 //当前页数
      }
    },
    created(){
      this.getData();
    },
    methods:{
      getData(){ //获取用户列表数据
        let o = {
          "model": "AdminUsers",
          "limit":10,
          "offset": this.curPage - 1,
          "orderBy":"create_time desc",
          "with":{
          },
          "condition":{
            is_delete:0
          },
          "search":{
          }
        };
        getDateList(o).then((data) => {
          console.log(data);
          this.list = data.data.data.data;
          this.pageCount = data.data.data.count - 0;
        })
      },
      pageChange(e){
        this.curPage = e;
        this.getData();
      },
      toggleAccount(status,id){ //启用/禁用账号
        let o = {
          "model": "AdminUsers",
          "data": {
            "status": Number(status)
          },
          update_condition:{
            "id":id
          }
        };
        createData(o).then((data) => {
          if(data.data.code == 200){
            this.getData();
            this.$message({
              type: 'success',
              message: '操作成功!'
            });
          }else{
            this.$message({
              type: 'error',
              message: '操作失败'
            });
          }
        })
      },
      toEdit(id){ //跳转到编辑
        this.$router.push('/adminEdit?id=' + id);
      },
      adminDelete(id){ //账号删除操作
        this.$confirm('要删除这个账号吗?','提示',{
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'info '
        }).then(() => {
          this.delFn(id);
        });
      },
      delFn(id){ //删除请求
        let o =  {
          "model": "AdminUsers",
          "data": {
            "is_delete":"1"
          },
          update_condition:{
            "id":id
          }
        };
        createData(o).then((data) => {
          if(data.data.code == 200){
            this.applySwitch = false;
            this.getData();
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
          }else{
            this.$message({
              type: 'error',
              message: '删除失败'
            });
          }
        })
      }
    }
  }
</script>
